package com.rafkind.resrev.data;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.jasypt.util.password.BasicPasswordEncryptor;

@Entity
@Table(name="users")
public class User implements Serializable {  
  public static final String DEFAULT_ADMIN_LOGIN_NAME = "admin";
  public static final String DEFAULT_ADMIN_PASSWORD = "admin";
  
  private static final long serialVersionUID = 1L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;
  
  @Column(nullable=false, unique=true)
  private String loginName;
  private String password;
  
  private String name;
  private String email;
  private String phone;
  private String comment;
  
  @Column(name="is_admin")
  private boolean admin;
  
  @ManyToOne
  private User authorizor;
  
  private Color color;

  public User() {
    color = Color.DEFAULT;
  }  
  
  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public boolean isAdmin() {
    return admin;
  }

  public void setAdmin(boolean admin) {
    this.admin = admin;
  }

  public User getAuthorizor() {
    return authorizor;
  }

  public void setAuthorizor(User authorizor) {
    this.authorizor = authorizor;
  }

  public Color getColor() {
    return color;
  }

  public void setColor(Color color) {
    this.color = color;
  }

  public String getComment() {
    return comment;
  }

  public void setComment(String comment) {
    this.comment = comment;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getLoginName() {
    return loginName;
  }

  public void setLoginName(String loginName) {
    this.loginName = loginName;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }  

  @Override
  public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof User)) {
      return false;
    }
    User other = (User) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return name;
  }
  
  public boolean isNew() {
    return id == null || id == 0;
        
  }
  
  public static User createDefaultAdminUser() {
    
    User u = new User();
    
    u.admin = true;
    u.authorizor = null;
    u.color = Color.DEFAULT;
    u.comment = "Default Admin User";
    u.email = "dave@rafkind.com";
    u.loginName = DEFAULT_ADMIN_LOGIN_NAME;
    u.name = "Default Admin User";
    u.password = new BasicPasswordEncryptor()
        .encryptPassword(DEFAULT_ADMIN_PASSWORD);
    u.phone = "n/a";
    
    return u;
  }
  
  public static User createSentinelUser() {
    User u = new User();
    
    u.admin = false;
    u.authorizor = null;
    u.color = Color.DEFAULT;
    u.comment = "";
    u.email = "";
    u.id = -1;
    u.loginName = "";
    u.name = "- None -";
    u.password = "";
    u.phone = "";
    
    return u;
  }
}